var express = require('express');
var router = express.Router();
const {sendMail
} = require("../public/js/library/getCode");

const fs=require('fs');
const path=require('path');

/* GET users listing. */
router.get('/getcode', function(req, res, next) {
//  let email=req.query.email
// //  console.log(email);
// let code=sendMail(email);
fs.writeFile(path.join(__dirname,'..','temp',req.query.email),sendMail(req.query.email),'utf-8',err=>{
  if(err) throw err;
  console.log('邮件已发送');
  res.send({
    success:1,
    msg:"邮件已发送",
    email:req.query.email,
  })
})
});
router.get('/checkcode',function(req,res,next){
fs.readFile(path.join(__dirname,'..','temp',req.query.email),'utf-8',(err,data)=>{
  if(err){ console.log(err);
  res.send({
    success: 0,
    message: '请勿重复发送',
    温馨提示: '您已经发送过啦',
  });
} else if(req.query.code === data) {
  res.send({
    success: 1,
    message: '验证成功',
    
  });
  //验证成功后让验证码失效(删除该验证码-》删除文件)
  
  fs.unlink(
    path.join(__dirname, '..', 'temp', req.query.email),
    (err) => {
      if (err) console.log(err);
      console.log('验证成功 文件已删除');
    }

  );
} else {
  res.send({
    success: 0,
    message: '验证码错误',
    错误码: '1005',
    
  });
}
})
});
const query =require('../conn/conn');
router.get('/login',async (req,res,next)=>{
const {username,password} =req.body;
//查询数据库 异步操作
let result = await query(`select * from usershuzhent9000 where username='${username}' and password='${password}'` );
console.log(result);
})

module.exports = router;
